<?php

namespace TpAmis\Traits;

use TpAmis\Renderers\BaseApi;

trait QueryPathTrait
{
    /**
     * 列表获取数据
     *
     * @return BaseApi
     */
    public function getListGetDataPath()
    {

        return BaseApi::make()->method('post')->url(admin_url($this->queryPath));

    }

    /**
     * 导出
     *
     * @return BaseApi
     */
    public function getExportPath()
    {
        return BaseApi::make()->method('post')->url(adminUrl('export'));

    }

    /**
     * 删除
     *
     * @return BaseApi
     */
    public function getDeletePath($primaryKey = 'id')
    {
        return BaseApi::make()->method('post')->url(adminUrl('delete', [$primaryKey => '${'.$primaryKey.'}']));

    }

    /**
     * 批量删除
     *
     * @return BaseApi
     */
    public function getBulkDeletePath()
    {
        return BaseApi::make()->method('post')->url(adminUrl('delete', ['ids' => '${ids}']));

    }

    /**
     * 编辑页面
     *
     * @return string
     */
    public function getEditPath($path = 'create', $vars = [], $suffix = true, $domain = false)
    {
        return adminUrl($path, $vars, $suffix, $domain);
    }

    /**
     * 编辑 获取数据
     * @param mixed $primaryKey
     * @return BaseApi
     */
    public function getEditGetDataPath($primaryKey = 'id')
    {
        return BaseApi::make()->method('get')->url(adminUrl('edit', [$primaryKey => '${'.$primaryKey.'}']));
    }

    /**
     * 详情页面
     *
     * @return string
     */
    public function getShowPath($primaryKey = 'id')
    {
        return adminUrl('detail', [$primaryKey => '${'.$primaryKey.'}']);
    }

    /**
     * 编辑保存
     * @param string $primaryKey 主键名称
     * @return BaseApi 
     */
    public function getUpdatePath($primaryKey = 'id')
    {

        return BaseApi::make()->method('post')->url(adminUrl('edit', [$primaryKey => '${'.$primaryKey.'}']));
    }

    /**
     * 快速编辑
     * @return \TpAmis\Renderers\BaseApi
     */
    public function getQuickEditPath()
    {
        $quickSaveApi = amis()->BaseApi()->method('post')
            ->url(adminUrl('modify'));
        return $quickSaveApi;
    }

    public function getQuickEditItemPath()
    {
        $quickSaveApi = amis()->BaseApi()->method('post')
            ->data([
                'ids' => '${id}',
                'rowsDiff' => '${rowsDiff}'
            ])
            ->url(adminUrl('modify'));
        return $quickSaveApi;
    }

    /**
     * 详情 获取数据
     *
     * @return string
     */
    public function getShowGetDataPath($primaryKey = 'id')
    {
        $path = $this->queryPath;

        if (blank(request()->route()->signatureParameters())) {
            $path .= '/${'.$primaryKey.'}';
        }

        return admin_url($path.'?_action=getData');
    }

    /**
     * 新增页面
     *
     * @return string
     */
    public function getCreatePath($path = 'create', $vars = [], $suffix = true, $domain = false)
    {
        return adminUrl($path, $vars, $suffix, $domain);
    }

    /**
     * 新增 保存
     *
     * @return string
     */
    public function getStorePath()
    {
        return 'post:'.adminUrl('create');
    }

    /**
     * Determine if a given string contains a given substring.
     *
     * @param  string  $haystack
     * @param  string|iterable<string>  $needles
     * @param  bool  $ignoreCase
     * @return bool
     */
    public static function contains($haystack, $needles, $ignoreCase = false)
    {
        if ($ignoreCase) {
            $haystack = mb_strtolower($haystack);
        }

        if (! is_iterable($needles)) {
            $needles = (array) $needles;
        }

        foreach ($needles as $needle) {
            if ($ignoreCase) {
                $needle = mb_strtolower($needle);
            }

            if ($needle !== '' && str_contains($haystack, $needle)) {
                return true;
            }
        }

        return false;
    }
    /**
     * 列表
     *
     * @return string
     */
    public function getListPath()
    {
        $path = $this->queryPath;

        if ($this->contains($this->queryPath, '/create')) {
            $path = str_replace('/create', '', $path);
        }

        if ($this->contains($this->queryPath, '/edit')) {
            $_path = explode('/', $path);
            array_pop($_path);
            array_pop($_path);
            $path = implode('/', $_path);
        }

        return '/'.trim($path, '/');
    }
}
